package com.ocom.aiotweb.controller;


import com.ocom.aiotweb.service.AccessDeviceService;
import com.ocom.aiotweb.service.ConsumeDeviceService;
import com.ocom.aiotweb.vo.accessdevice.AddVo;
import com.ocom.aiotweb.vo.accessdevice.DeleteVo;
import com.ocom.aiotweb.vo.accessdevice.OnShowVo;
import com.ocom.aiotweb.vo.accessdevice.UpgradeVo;
import com.ocom.common.constant.AiotUrl;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@RestController
public class AccessDeviceController {
    private final Logger log = LoggerFactory.getLogger(AccessDeviceController.class);

    @Autowired
    private AccessDeviceService accessDeviceService;


    // 门禁设备列表（AccessDevice/onShow）
    @PostMapping(AiotUrl.URL_Aiot_AccessDeviceOnShow)
    public Result AccessDeviceOnShow(@Validated @RequestBody OnShowVo onShowVo){
        return accessDeviceService.AccessDeviceOnShow(onShowVo);
    }

    // 门禁设备新增（AccessDevice/add）
    @PostMapping(AiotUrl.URL_Aiot_AccessDeviceAdd)
    public Result AccessDeviceAdd(String saasNo,Long comId,Long agentId,Long commMode,Integer deviceType,Integer deviceVerType,@RequestPart("file") MultipartFile file){

        return accessDeviceService.AccessDeviceAdd(saasNo,comId,agentId,commMode,deviceType,deviceVerType,file);
    }

    // 门禁设备删除（AccessDevice/delete）
    @PostMapping(AiotUrl.URL_Aiot_AccessDeviceDelete)
    public Result AccessDeviceDelete(@Validated @RequestBody DeleteVo deleteVo){
        if (deleteVo.getDeviceNo() == null){
            log.error("删除设备出厂编号为空");
            return ResultUtil.error(ResultEnum.Err_203,"删除设备出厂编号为空");
        }
        return accessDeviceService.AccessDeviceDelete(deleteVo);
    }


    /**
     * 导入模板下载（AccessDevice/importDeviceTemplate）
     * */
    @GetMapping(AiotUrl.URL_Aiot_AccessDeviceImportDeviceTemplate)
    public void AccessDeviceImportDeviceTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<AddVo> util = new ExcelUtil<AddVo>(AddVo.class);
        util.importTemplateExcel(response, "设备数据");
    }

    // 门禁升级版本列表（AccessDevice/upgradeList）
    @GetMapping(AiotUrl.URL_Aiot_AccessDeviceUpgradeList)
    public Result ConsumeDeviceUpgradeList(String appVer,Integer deviceType){
        if (appVer == null ){
            log.error("设备版本为空");
            return ResultUtil.error(ResultEnum.Err_203,"设备版本为空");
        }
        if ( deviceType == null){
            log.error("设备类型为空");
            return ResultUtil.error(ResultEnum.Err_203,"设备类型为空");
        }
        return accessDeviceService.AccessDeviceUpgradeList(appVer,deviceType);
    }


    // 门禁设备升级（AccessDevice/upgrade）
    @PostMapping(AiotUrl.URL_Aiot_AccessDeviceUpgrade)
    public Result AccessDeviceUpgrade(@Validated  @RequestBody UpgradeVo upgradeVo){
        if (upgradeVo.getDeviceNo() == null ){
            log.error("设备编号为空");
            return ResultUtil.error(ResultEnum.Err_203,"设备编号为空");
        }
        if ( upgradeVo.getUpgradeVer() == null){
            log.error("版本号为空");
            return ResultUtil.error(ResultEnum.Err_203,"版本号为空");
        }
        return accessDeviceService.AccessDeviceUpgrade(upgradeVo);
    }


}
